package com.debao.wechat;


import com.debao.common.annotation.Anonymous;
import com.debao.common.constant.WechatConstants;
import com.debao.common.core.domain.R;
import com.debao.common.core.domain.entity.SysUser;
import com.debao.common.utils.wechat.WeixinCheckoutUtil;
import com.debao.common.wechat.WechatUtils;
import com.debao.system.domain.wechat.WeChatMPService;
import com.debao.system.domain.wechat.WechatAccountService;
import com.debao.system.service.IConfigService;
import com.debao.system.service.ICourseCardOrderService;
import com.debao.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 公众号服务器验证接口
 */
@RestController
@RequestMapping("/wechat")
@CrossOrigin
@Slf4j
public class ApiWechat {


    @Autowired
    private WeChatMPService weChatMPService;

    @Autowired
    private ISysUserService userService;

    @Resource
    private IConfigService configService;


    @Resource
    private ICourseCardOrderService courseCardOrderService;


    /**
     * 微信公众号API的Service
     */
    private final WxMpService wxMpService;

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiWechat.class);

    ApiWechat(WxMpService wxMpService) {
        this.wxMpService = wxMpService;
    }


    /**
     * 微信服务器验证接口
     *
     * @param request
     * @param response
     */
    @Anonymous
    @GetMapping("/mp/serverCheck")
    public void checkToken(HttpServletRequest request, HttpServletResponse response) {
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");

        PrintWriter out = null;
        try {
            out = response.getWriter();
            if (WeixinCheckoutUtil.checkSignature(signature, timestamp, nonce)) {
                LOGGER.info("微信加密签名:" + signature + ";时间戳:" + timestamp + ";随机数:" + nonce);
                out.print(echostr);
            }
        } catch (IOException e) {
            LOGGER.error("微信服务器验证接口异常", e);
        } finally {
            if (out != null) {
                out.close();
            }
            out = null;
        }
    }

    /**
     * 处理微信服务器发来的消息
     *
     * @param request
     * @return
     * @throws Exception
     */
    @Anonymous
    @PostMapping("/mp/serverCheck")
    public String doPost(HttpServletRequest request) throws Exception {
        // 调用核心服务类接收处理请求
        return weChatMPService.processRequest(request);
    }


    /**
     * 发送微信模板信息
     *
     * @param
     * @return 是否推送成功
     */
    @Anonymous
    @GetMapping("/mp/sendWxMsg")
    public void SendWxMsg(Long userId) {

    }

    @Anonymous
    @GetMapping("/test")
    public void text(@RequestParam String content,@RequestParam String openId) {
        String token = configService.selectValueByKey(WechatConstants.APP_TOKEN);
        WechatUtils.weiXinCheckContent(token, content,openId);
    }
}
